/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ies.domain.model.repository.facades;

import com.ies.domain.model.Permissao;
import com.ies.domain.model.Usuario;
import com.ies.domain.model.repository.util.AbstractFacade;
import com.ies.domain.model.util.ModelInterface;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;

/**
 *
 * @author jackson
 */
public class UsuarioFacade extends AbstractFacade<Usuario> {

    public UsuarioFacade() {
        super(Usuario.class);
        carregaRelacionamentos = true;
    }

    @Override
    public void create(Usuario entity) {
        EntityManager entityManager = getEntityManager();
        try {
            entityManager.getTransaction().begin();
            entity.getEndereco().setUsuario(entity);
            if (entity.getEndereco().getId() == null || entity.getEndereco().getId() == 0) {
                entityManager.persist(entity.getEndereco());
            } else {
                entityManager.merge(entity.getEndereco());
            }
            if(entity.getPermissaos()!= null){
                for(Permissao p : entity.getPermissaos()){
                    if(p.getId()!= null){
                        entityManager.merge(p);
                    }else{
                        entityManager.persist(p);
                    }
                }
            }
            entityManager.persist(entity);
            entityManager.getTransaction().commit();
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, null, ex);
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        } finally {
            entityManager.close();
        }

    }

    @Override
    public void edit(Usuario entity) {
        EntityManager entityManager = getEntityManager();
        try {
            entityManager.getTransaction().begin();
            entity.getEndereco().setUsuario(entity);
            if (entity.getEndereco().getId() == null || entity.getEndereco().getId() == 0) {
                entityManager.persist(entity.getEndereco());
            } else {
                entityManager.merge(entity.getEndereco());
            }
            if(entity.getPermissaos()!= null){
                for(Permissao p : entity.getPermissaos()){
                    if(p.getId()!= null){
                        entityManager.merge(p);
                    }else{
                        entityManager.persist(p);
                    }
                }
            }
            entityManager.merge(entity);
            entityManager.getTransaction().commit();
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, null, ex);
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        } finally {
            entityManager.close();
        }

    }

    @Override
    public Usuario carregaLazy(Usuario object) {
        
        if(object.getPermissaos()!= null){
            object.getPermissaos().size();
        }
        return object;
    }

    @Override
    public void remove(Usuario entity) {
        EntityManager entityManager = getEntityManager();
        try {
            entityManager.getTransaction().begin();
            entity = entityManager.find(Usuario.class,entity.getId());
            
            if(entity.getEndereco()!= null){
                entityManager.remove(entity.getEndereco());
            }
            entityManager.remove(entity);
            entityManager.getTransaction().commit();
        } catch (Exception ex) {
            Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, null, ex);
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
        } finally {
            entityManager.close();
        }
    }
    
    
    
}